package com.zhk.ScriptManagement.controller;

import com.zhk.ScriptManagement.entity.User;
import com.zhk.ScriptManagement.entity.vo.UserVo;
import com.zhk.ScriptManagement.service.UserService;
import com.zhk.ScriptManagement.util.CodeMsg;
import com.zhk.ScriptManagement.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

/**
 * @Author: zhouhongkun
 * @Description: 用户
 * @Date: 2021/4/4 18:55
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 添加用户
     * @param user 用户
     * @return
     */
    @PostMapping("addUser")
//    @RequiresPermissions({"1"})
    public Result addUser(@RequestBody User user){
        if(!Optional.ofNullable(user).isPresent()){
            return Result.error(CodeMsg.PARAMETER_ISNULL,"参数不能为空");
        }
        return userService.addUser(user);
    }

    /**
     * 更新用户
     * @param user
     * @return
     */
    @PostMapping("updateUser")
//    @RequiresPermissions({"1"})
    public Result updateUser(@RequestBody User user){
        if(!Optional.ofNullable(user).isPresent()){
            return Result.error(CodeMsg.PARAMETER_ISNULL,"参数不能为空");
        }
        return userService.updateUser(user);
    }

    /**
     * 获取用户列表
     * @param size
     * @param num
     * @return
     */
    @GetMapping("getUsers")
    public Result getUsers(@RequestParam(value = "size",defaultValue = "10",required = false) String size,
                           @RequestParam(value = "num",defaultValue = "1",required = false) String num,
                           @RequestParam(value = "realName",defaultValue = "" ,required = false) String realName){
        return userService.getUsers(size,num,realName);
    }

    /**
     * 根据用户id查询用户信息
     * @param userId
     * @return
     */
    @GetMapping("getUserByUserId")
    public Result getUserByUserId(@RequestParam Integer userId){
        if(!Optional.ofNullable(userId).isPresent()){
            return Result.error(CodeMsg.PARAMETER_ISNULL,"参数不能为空");
        }
        return userService.getUserByUserId(userId);
    }

}
